/**
* -------------------------------------------------------------
* @author  jiao
* @date: 2021-12-31 15:04:01
* @description: Student serviceImpl
* -------------------------------------------------------------
* @version 1.0.0
* @Copyright (c) 中科星图-版权所有
*/

package com.geovis.boot.service.demo.file.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.boot.exception.CheckException;
import com.geovis.boot.service.demo.file.entity.Performance;
import com.geovis.boot.service.demo.file.entity.Student;
import com.geovis.boot.service.demo.file.mapper.StudentMapper;
import com.geovis.boot.service.demo.file.service.IPerformanceService;
import com.geovis.boot.service.demo.file.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 学生接口
 * @author jiao
 */
@Slf4j
@Service
public class IStudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
//    private static Logger log1 = LoggerFactory.getLogger(IStudentServiceImpl.class);
    @Autowired
    private IPerformanceService performanceService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer id) {

        //1、删除学生成绩
        this.performanceService.remove(new QueryWrapper<Performance>().eq("student_id",id));
        //2、删除学生
        int i = this.baseMapper.deleteById(id);
        //模拟异常
        try {
            int a = 1/0;
        }catch (Exception e){
            log.error("触发异常"+e.getMessage());
            throw new CheckException("触发异常"+e);

        }

        return i;
    }

    @Override
    public int deleteByIdNoTransactional1(Integer id) {
        //1、删除学生成绩
        try {
            boolean student_id = this.performanceService.removeException(id);
        }catch (Exception e){
            log.error("删除学生异常，异常编号："+id);
        }
        //2、删除学生
        this.baseMapper.deleteById(id);
        return 0;
    }

    @Override
//    @Transactional(rollbackFor = Exception.class) 若配了此异常都回滚，
//    @Transactional(rollbackFor = Exception.class)
    @Transactional
    public int deleteByIdNoTransactional2(Integer id) {
        this.performanceService.deleteByStudentIdNewTransactional(id);
        //模拟异常
        int i = 1/0;
        //1、删除学生
        this.baseMapper.deleteById(id);
        //2、删除学生成绩

        return 0;
    }

    @Override
    @Transactional
    public int deleteByIdRollback(Integer id) {
        //1、删除学生成绩
        this.performanceService.remove(new QueryWrapper<Performance>().eq("student_id",id));
        //2、删除学生
        try{

            int a = this.baseMapper.deleteById(id);
            throw new RuntimeException("lalala");
        }catch (Exception e){
            //手动事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return 0;
    }
}
